//
// Copyright (c) 2009 All Right Reserved
//
// vl
//
// 2018-12-01
// Contains ...
using System;
using System.Windows;
using System.Windows.Media;
namespace LargoPanels.Editor
{
/// A base chart master.
public partial class EditorSpace : FrameworkElement
{
#region Public properties - DrawingVisual
///
/// Gets or sets the drawing visual.
///
///
/// The drawing visual.
///
public DrawingVisual DrawingVisual { get; set; }
///
/// Gets the number of visual child elements within this element. Mandatory overrides for VisualChildrenCount property.
///
protected override int VisualChildrenCount => 1;
///
/// Overrides , and returns a child at the specified index from a collection of child elements.
///
/// The zero-based index of the requested child element in the collection.
///
/// The requested child element. This should not return null; if the provided index is out of range, an exception is thrown.
///
/// Argument Out Of Range Exception
protected override Visual GetVisualChild(int index) {
if (index != 0) {
throw new ArgumentOutOfRangeException();
}
return this.children[index];
}
#endregion
///
/// Creates the drawing visual.
///
/// Returns value.
private DrawingVisual CreateDrawingVisual() { //// int stationObjectSize, double actualWidth, double actualHeight) {
//// initialize canvasGrid
this.DrawingVisual = new DrawingVisual();
//// end using clause
return this.DrawingVisual;
}
/*
///
/// Create a DrawingVisual that contains a rectangle.
///
///
private DrawingVisual CreateDrawingVisualRectangle() {
DrawingVisual drawingVisual = new DrawingVisual();
// Retrieve the DrawingContext in order to create new drawing content.
DrawingContext drawingContext = drawingVisual.RenderOpen();
// Create a rectangle and draw it in the DrawingContext.
Rect rect = new Rect(new System.Windows.Point(160, 100), new System.Windows.Size(320, 80));
drawingContext.DrawRectangle(System.Windows.Media.Brushes.LightBlue, (System.Windows.Media.Pen)null, rect);
// Persist the drawing content.
drawingContext.Close();
return drawingVisual;
}
*/
///
/// Draws the space rectangle.
///
/// The drawing context.
private void DrawSpaceRectangle(DrawingContext drawingContext) {
var point = new Point(this.LeftSpace - 10, this.TopSpace - 10);
var rectangle = new Rect(point, new Size(this.MaxLeft, this.MaxTop));
Pen pen = new Pen(Brushes.Ivory, 1.0);
var contentBrush = Brushes.Ivory;
drawingContext.DrawRectangle(contentBrush, pen, rectangle);
}
///
/// When overridden in a derived class, participates in rendering operations that are directed by the layout system. The rendering instructions for this element are not used directly when this method is invoked, and are instead preserved for later asynchronous use by layout and drawing.
///
/// The drawing instructions for a specific element. This context is provided to the layout system.
protected override void OnRender(DrawingContext drawingContext) {
/* using (DrawingContext dc = this.EditorSpace.DrawingVisual.RenderOpen()) {
drawingContext.DrawRectangle(Brushes.Blue, new Pen(Brushes.Yellow, 3), new Rect(new Point(100, 100), new Point(400, 200)));
}; */
//// Set coordinates and sizes of cells + shifts according to scrollbars
this.PlaceCells();
//// this.DrawingVisual //// this.children.Clear();
//// this.children = new VisualCollection(this);
//// this.CreateDrawingVisual();
//// this.RemoveVisualChild();
//// this.RemoveLogicalChild();
//// Draw visible area
this.DrawSpaceRectangle(drawingContext);
this.DrawCells(drawingContext);
}
#region Public methods - Draw cells on Canvas
///
/// Places the cells.
///
private void PlaceCells() {
foreach (var editorLine in this.EditorLines) {
foreach (var cell in editorLine.ContentCells) {
cell.Left = this.LeftSpace + this.LeftMargin - this.LeftScroll + ((cell.Point.BarNumber - 1) * SeedSize.CurrentWidth);
cell.Top = this.TopSpace + this.TopMargin - this.TopScroll + (cell.Point.LineIndex * SeedSize.CurrentHeight);
}
foreach (var cell in editorLine.GroupCells) {
cell.Width = (cell.InnerCells.Count * SeedSize.CurrentWidth) - SeedSize.BasicMargin;
cell.Height = SeedSize.CurrentHeight - SeedSize.BasicMargin;
var previousWidth = (cell.Point.BarNumber - 1) * SeedSize.CurrentWidth;
cell.Left = this.LeftSpace + this.LeftMargin - this.LeftScroll + previousWidth;
if (cell.Left < this.LeftSpace + this.LeftMargin) {
cell.Width = cell.Width - (this.LeftSpace + this.LeftMargin - cell.Left);
cell.Left = this.LeftSpace + this.LeftMargin;
}
var previousHeight = cell.Point.LineIndex * SeedSize.CurrentHeight;
cell.Top = this.TopSpace + this.TopMargin - this.TopScroll + previousHeight;
if (cell.Top < this.TopSpace + this.TopMargin) {
cell.Height = cell.Height - (this.TopSpace + this.TopMargin - cell.Top);
cell.Top = this.TopSpace + this.TopMargin;
}
if (cell.Left + cell.Width > this.MaxLeft) {
cell.Width = this.MaxLeft - cell.Left - SeedSize.BasicMargin;
}
if (cell.Top + cell.Height > this.MaxTop) {
cell.Height = this.MaxTop - cell.Height;
if (cell.Height> SeedSize.BasicHeight) {
cell.Height = SeedSize.BasicHeight;
}
}
}
}
foreach (var cell in this.BarCells) {
cell.Left = this.LeftSpace + this.LeftMargin - this.LeftScroll + (cell.BarIndex * SeedSize.CurrentWidth);
cell.Top = this.TopSpace;
}
foreach (var cell in this.LineCells) {
cell.Left = this.LeftSpace;
cell.Top = this.TopSpace + this.TopMargin - this.TopScroll + (cell.LineIndex * SeedSize.CurrentHeight);
}
if (this.cornerCell != null) {
this.cornerCell.Left = this.LeftSpace; //// - this.LeftScroll;
this.cornerCell.Top = this.TopSpace; //// - this.TopScroll;
this.cornerCell.Height = (2 * SeedSize.CurrentHeight) - SeedSize.BasicMargin;
this.cornerCell.Width = (2 * SeedSize.CurrentWidth) - SeedSize.BasicMargin;
}
}
///
/// Draws the cells.
///
/// The drawing context.
private void DrawCells(DrawingContext drawingContext) {
foreach (var track in this.EditorLines) {
/*
foreach (var cell in track.EditorCells) {
if (cell.Left + cell.Width < this.LeftSpace + this.LeftMargin || cell.Left > this.MaxLeft) {
continue;
}
if (cell.Top + cell.Height < this.TopSpace + this.TopMargin || cell.Top > this.MaxTop) {
continue;
}
cell.DrawCell(drawingContext, false);
}
*/
foreach (var cell in track.GroupCells) {
/*
if (cell.Left < this.LeftSpace + this.LeftMargin) {
cell.Left = this.LeftSpace + this.LeftMargin;
}
if (cell.Top < this.TopSpace + this.TopMargin) {
cell.Top = this.TopSpace + this.TopMargin;
}
*/
if (cell.Left + cell.Width < this.LeftSpace + this.LeftMargin || cell.Left > this.MaxLeft) {
continue;
}
if (cell.Top + cell.Height < this.TopSpace + this.TopMargin || cell.Top > this.MaxTop) {
continue;
}
cell.DrawCell(drawingContext, false);
}
}
foreach (var cell in this.BarCells) {
if (cell.Left + cell.Width < this.LeftSpace + this.LeftMargin || cell.Left + SeedSize.CurrentWidth > this.MaxLeft) {
continue;
}
cell.DrawCell(drawingContext, true);
}
foreach (var cell in this.LineCells) {
if (cell.Top + cell.Height < this.TopSpace + this.TopMargin || cell.Top + SeedSize.CurrentHeight > this.MaxTop) {
continue;
}
cell.DrawCell(drawingContext, true);
}
this.cornerCell?.DrawCell(drawingContext, true);
}
#endregion
}
}